package iosr11.gbase.dao;

import iosr11.data.Attendence;
import iosr11.data.Event;
import iosr11.data.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseAttributesExtension;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.gdata.data.PlainTextConstruct;

public class AttendenceDAOImpl extends GenericDAOImpl<Attendence> implements AttendenceDAO{

	private static String TYPE = "attendence";
	private static String STUDENT_REF = "student";
	private static String EVENT_REF = "event";
	private static String N_PRESENCE = "presence";
	private Logger log = Logger.getLogger("service-log");
	
	public AttendenceDAOImpl(){
		
	}
	
	@Override
	public List<Attendence> getEventAttendence(Event event) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault()
				.getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance()
				.getGoogleBaseQueryResult(query, EVENT_REF,
						event.getId());
		if (feed == null) {
			log.info("brak rekordow w bazie majacych referencje do eventu: "+event.getId());
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak obecnosci dla eventu: "+event.getId());
			return null;
		}
		List<Attendence> result = new ArrayList<Attendence>();
		for (GoogleBaseEntry entry : feed.getEntries()) {
			Attendence tmp = this.rewrite(entry);
			tmp.setEvent(event);
			result.add(tmp);
		}
		log.info("znaleziono "+result.size()+" obecnosci nalezacych do eventu: "+event.getId());
		return result;
	}

	@Override
	public List<Attendence> getStudentsAttendence(Student student) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault()
				.getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance()
				.getGoogleBaseQueryResult(query, STUDENT_REF,
						student.getId());
		if (feed == null) {
			log.info("brak rekordow w bazie majacych referencje do studenta: "+student.getName());
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak ocen dla studenta: "+student.getName());
			return null;
		}
		
		List<Attendence> result = new ArrayList<Attendence>();
		for (GoogleBaseEntry entry : feed.getEntries()) {
			Attendence tmp = this.rewrite(entry);
			tmp.setStudent(student);
			result.add(tmp);
		}
		log.info("znaleziono "+result.size()+" obecnosci nalezacych do studenta: "+student.getName());
		return result;
	}

	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}

	@Override
	public GoogleBaseEntry rewrite(Attendence entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setTitle(new PlainTextConstruct(TYPE));
		entry.setDraft(true);
		GoogleBaseAttributesExtension ext = entry.getGoogleBaseAttributes();
		ext.setItemType(TYPE);
		ext.addTextAttribute(STUDENT_REF, entity.getStudent().getId());
		ext.addTextAttribute(EVENT_REF, entity.getEvent().getId());
		ext.addIntAttribute(N_PRESENCE, entity.getPressenceI());
		return entry;
	}

	@Override
	public Attendence rewrite(GoogleBaseEntry entry) {
		Attendence entity = new Attendence();
		entity.setId(entry.getId());
		entity.setPressence(entry.getGoogleBaseAttributes().getIntAttribute(N_PRESENCE));
		Student student = new Student();
		Event event = new Event();
		if (entry.getGoogleBaseAttributes().getTextAttribute(STUDENT_REF)==null){
			log.warning("brak referencji na studenta - null");
			return null;
		}
		student.setId(entry.getGoogleBaseAttributes().getTextAttribute(STUDENT_REF));
		if (entry.getGoogleBaseAttributes().getTextAttribute(EVENT_REF)==null){
			log.warning("brak referencji na event - null");
			return null;
		}
		event.setId(entry.getGoogleBaseAttributes().getTextAttribute(EVENT_REF));
		entity.setEvent(event);
		entity.setStudent(student);
		return entity;
	}

}
